<?php

namespace app\system\controller;

use app\common\model\AttachmentGroupModel;
use app\common\model\AttachmentModel;
use think\facade\Filesystem;

class AttachmentController extends BaseController
{

  /**
   * 无需登录的方法
   * @var array
   */
  protected $noLogin = [];

  /**
   * 不需要检测权限的方法
   * @var array
   */
  protected $noCheck = ['index', 'select', 'del', 'group'];

  /**
   * 权限映射
   * @var array
   */
  protected $mapCheck = [];

  protected $model;
  protected $groupModel;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model      = new AttachmentModel();
    $this->groupModel = new AttachmentGroupModel();
  }

  /**
   * 列表
   * @return array|string
   * @throws \Exception
   */
  public function index()
  {
    if ($this->request->isAjax()) {
      $data  = $this->getPage();
      if (!empty($data['rows'])) $data['rows'] = $this->model->_formatList($data['rows']);
      return $data;
    }
    return $this->fetch();
  }

  /**
   * 选择图片
   * @return array|string
   * @throws \Exception
   */
  public function select()
  {
    if ($this->request->isAjax()) {
      $map      = [];
      $fileType = $this->request->param('file_type');
      if (!empty($fileType)) $map[] = ['file_type', 'in', $fileType];
      $data = $this->getPage([], [], $map);
      if (!empty($data['rows'])) $data['rows'] = $this->model->_formatList($data['rows']);
      $data['sql'] = $this->model->getLastSql();
      return $data;
    }
    $fileType = $this->request->param('file_type/s');
    $groupList = $this->groupModel->order('show_order desc')->select()->toArray();
    $groupList = $this->groupModel->_formatList($groupList,$fileType);
    $this->assign('group_list', $groupList);
    return $this->fetch();
  }

  /**
   * 删除附件
   */
  public function del()
  {
    try {
      $ids = $this->request->param('id');
      if (empty($ids)) $this->error('请选择附件资源');
      $ids = explode(',', $ids);
      foreach ($ids as $id) {
        $info   = $this->model->where('id', $id)->find();
        $config = config("filesystem.disks.{$info['driver']}");
        $path   = str_replace($config['domain'] . '/upload', 'upload', $info['file_url']);
        $this->model->where('id', $id)->delete();
        if (Filesystem::disk($info['driver'])->has($path)) {
          Filesystem::disk($info['driver'])->delete($path);
        }
      }
    } catch (\Throwable $th) {
      $this->error($th->getMessage());
    }
    $this->success('操作成功');
  }

  /**
   * 分组相关操作
   * @return mixed
   * @throws \Exception
   */
  public function group()
  {
    $action = $this->request->param('action', 'list');
    switch ($action) {
      case 'list':
        $list = $this->groupModel->order('show_order desc')->select()->toArray();
        return $this->groupModel->_formatList($list);
        break;
      case 'add':
        try {
          $data = $this->request->param();
          $this->validate($data, [
            'group_name|分组名称' => 'require',
          ]);
          $this->groupModel->add($data);
        } catch (\Throwable $th) {
          $this->error($th->getMessage());
        }
        $this->success('操作成功');
        break;
      case 'edit':
        try {
          $data = $this->request->param();
          $this->validate($data, [
            'id|分组'           => 'require',
            'group_name|分组名称' => 'require',
          ]);
          $this->groupModel->edit($data);
        } catch (\Throwable $th) {
          $this->error($th->getMessage());
        }
        $this->success('操作成功');
        break;
      case 'del':
        try {
          $id = $this->request->param('id');
          $this->groupModel->del($id);
        } catch (\Throwable $th) {
          $this->error($th->getMessage());
        }
        $this->success('操作成功');
        break;
      case 'move':
        $groupId = $this->request->param('group_id/d');
        $ids     = $this->request->param('ids/s');
        $this->model->where('id', 'in', $ids)->update(['group_id' => $groupId]);
        $this->success('操作成功');
        break;
    }
  }
}
